package com.example.qxfw.nyqx.service.impl;

import com.example.qxfw.common.entity.ny.CropyieldFcst;
import com.example.qxfw.common.entity.ny.CropyieldHistory;
import com.example.qxfw.nyqx.dto.CltjDto;
import com.example.qxfw.nyqx.service.CltjService;
import com.example.qxfw.nyqx.service.CropyieldFcstService;
import com.example.qxfw.nyqx.service.CropyieldHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CltjServiceImpl implements CltjService {


    @Autowired
    private CropyieldHistoryService cropyieldHistoryService;

    @Autowired
    private CropyieldFcstService cropyieldFcstService;

    @Override
    public List<CltjDto> getCltj(String stationId, String cropName, String type, int year1, int year2) {

        List<CropyieldHistory> cropyieldHistories = cropyieldHistoryService.listByParams(stationId, cropName, type, year1, year2);

        List<CropyieldFcst> cropyieldFcsts = cropyieldFcstService.getList4(year1, year2, stationId, type, cropName);

        List<CltjDto> cltjDtos = new ArrayList<>();

        for (CropyieldHistory cropyieldHistory : cropyieldHistories) {
            for (CropyieldFcst cropyieldFcst : cropyieldFcsts) {
                if (cropyieldHistory.getYear() == cropyieldFcst.getYear()
                && cropyieldHistory.getStationid().equals(cropyieldFcst.getStationid())){
                    CltjDto cltjDto = new CltjDto();
                    cltjDto.setStationId(cropyieldHistory.getStationid());
                    cltjDto.setYear(cropyieldHistory.getYear());
                    cltjDto.setYieldHostory(cropyieldHistory.getYield());
                    cltjDto.setYieldFcst(cropyieldFcst.getYield());
                    cltjDtos.add(cltjDto);
                }
            }
        }

        return cltjDtos;
    }
}
